<?php
/**
 * This controller is used to process connect with facebook
 *
 * @author	huytbt
 * @date	2012-05-15
 * @version	1.0
 */
class ConnectController extends HController
{
	/**
	 * Returns the access rules for this controller.
	 * Override this method if you use the {@link filterAccessControl accessControl} filter.
	 * @return array list of access rules. See {@link CAccessControlFilter} for details about rule specification.
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users anything not specified
				'users'=>array('*'),
			),
		);
	}
	
	/**
	 * This controller is used to process when user login successful
	 */
	public function actionLogin()
	{
		Yii::import('application.modules.facebook.components.HFBUserIdentity');
		$identity = new HFBUserIdentity('facebook', 'facebook');
		$identity->authenticate();
		if($identity->errorCode === HFBUserIdentity::ERROR_NONE) {
			$duration = 3600*24*30; // 30 days
			Yii::app()->user->login($identity, $duration);
			
			$this->setRedirectOptions(array(
				"title" => FacebookModule::t('Login Success'),
				"message" => FacebookModule::t('The login was successful!'),
			));
		} else {
			$this->setRedirectOptions(array(
				"title" => FacebookModule::t('Login Failure'),
				"message" => FacebookModule::t('The login was failure!'),
			));
		}
		
		$this->redirect(Yii::app()->getModule('facebook')->loginUrl);
	}
	
	/**
	 * This controller is used to process when user logout successful
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		
		$this->setRedirectOptions(array(
			"title" => FacebookModule::t('Thanks'),
			"message" => FacebookModule::t('You are now signed out'),
		));
		
		$this->redirect(Yii::app()->homeUrl);
	}
}
